package com.lhy.admin.service;

import com.lhy.admin.mapper.RolePermissionMapper;
import com.lhy.admin.mapper.UserRoleMapper;
import com.lhy.admin.model.Permission;
import com.lhy.admin.model.RolePermission;
import com.lhy.admin.model.UserRole;
import com.lhy.admin.model.extend.RolePermissions;
import com.lhy.admin.util.CollectionUtil;
import com.lhy.commonweb.service.AbstractService;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
@Slf4j
public class RolePermissionService extends AbstractService<RolePermission, Integer> {

    @Autowired
    PermissionService permissionService;

    public RolePermissionService(@Qualifier("rolePermissionMapper") Mapper<RolePermission> mapper) {
        super(mapper);
    }

    @Transactional(rollbackFor = Exception.class)
    public void updateRolePermission(RolePermissions rolePermissions) {
        Integer roleId = rolePermissions.getRoleId();
        List<Integer> newPermissionIds = rolePermissions.getPermissionIds();
        log.debug("将更新权限的角色：{}", roleId);
        log.debug("新permissionIds:{}", CollectionUtil.toString(newPermissionIds));

        //查询原有已存在的permissionIds
        List<RolePermission> oldRolePermissions = mapper.select(new RolePermission().setRoleId(roleId));
        List<Integer> oldRolePermissionsIds = oldRolePermissions.stream().map(RolePermission::getPermId).collect(Collectors.toList());
        log.debug("已存在的permissionIds:{}", CollectionUtil.toString(oldRolePermissionsIds));

        //rolePermissionsIds
        List<Integer> deleteRolePermissionsIds = oldRolePermissions.stream()
                .filter(item -> !newPermissionIds.contains(item.getPermId()))
                .map(RolePermission::getId)
                .collect(Collectors.toList());
        log.debug("需要删除的permissionIds:{}", CollectionUtil.toString(deleteRolePermissionsIds));
        if (!deleteRolePermissionsIds.isEmpty()) {
            Example example = new Example(RolePermission.class);
            example.and().andIn(RolePermission.FieldEnum.ID.javaFieldName(), deleteRolePermissionsIds);
            mapper.deleteByExample(example);
        }

        Example example = new Example(Permission.class);
        example.and().andIn(Permission.FieldEnum.ID.javaFieldName(), newPermissionIds);
        List<Permission> permissions = permissionService.query(example);

        //需要新增的
        List<RolePermission> addRolePermissionIds = newPermissionIds.stream()
                .filter(item -> !oldRolePermissionsIds.contains(item))
                .map(aLong -> new RolePermission().setPermId(aLong).setRoleId(roleId).setPerm(getPemissionName(permissions, aLong)))
                .collect(Collectors.toList());
        log.debug("需要新增的permissionIds:{}", CollectionUtil.toString(addRolePermissionIds));
        addRolePermissionIds.forEach(mapper::insert);
    }

    private String getPemissionName(List<Permission> permissions, Integer permissionId) {
        Optional<String> first = permissions.stream()
                .filter(permission -> permissionId.equals(permission.getId()))
                .map(Permission::getName)
                .findFirst();
        return first.orElse("");
    }

}
